package com.fengqianrun.bootframework.service.file.impl;

import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.exp.impl.BasicException;
import com.aizuda.easy.security.server.EasySecurityServer;
import com.fengqianrun.bootframework.config.prop.IndexProp;
import com.fengqianrun.bootframework.domian.vo.UserVO;
import com.fengqianrun.bootframework.service.file.FileService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Resource
    private IndexProp indexProp;
    @Resource
    private UploadFileStrategy uploadFileStrategy;
    @Resource
    private EasySecurityServer easySecurityServer;

    @Override
    public Rep<String> uploadLocal(MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        String string = UUID.randomUUID().toString().replace("-", "");
        String local = indexProp.getFiles().getSavePath() + string + (name.contains(".") ? name.substring(name.indexOf(".")): "");
        File fil = new File(local);
        InputStream inputStream = file.getInputStream();
        FileUtils.copyInputStreamToFile(inputStream, fil);
        inputStream.close();
        return Rep.ok(local);
    }

    @Override
    public Rep<String> uploadPublic(MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        String string = UUID.randomUUID().toString().replace("-", "");
        String local = indexProp.getFiles().getSavePath() + string + (name.contains(".") ? name.substring(name.indexOf(".")): "");
        String publicP = indexProp.getFiles().getFullPath() + string + name.substring(name.indexOf("."));
        File fil = new File(local);
        InputStream inputStream = file.getInputStream();
        FileUtils.copyInputStreamToFile(inputStream, fil);
        inputStream.close();
        return Rep.ok(publicP);
    }

    @Override
    public Rep<String> uploadStrategy(MultipartFile file, String path, HttpServletRequest httpServletRequest) throws IOException, BasicException {
        String token = httpServletRequest.getHeader("token");
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        String name = file.getOriginalFilename();
        String local = indexProp.getFiles().getSavePath() + path + "/"+ name;
        File fil = new File(local);
        InputStream inputStream = file.getInputStream();
        UserVO userVO = null;
        if(StrUtil.isNotEmpty(token)) {
            userVO = (UserVO) easySecurityServer.getAuthUser(token);
        }
        if(uploadFileStrategy.exec(local,file, parameterMap,userVO)) {
            FileUtils.copyInputStreamToFile(inputStream, fil);
        }
        inputStream.close();
        return Rep.ok(local);
    }
}
